package cn.stronglink.assetmanage.business.search;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.business.search.presenter.IAssetDetailView;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.ModelDao;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Department;
import cn.stronglink.assetmanage.entity.Model;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.exceptions.BusinessErrorException;

public class DetailsUtils {

    // 获取资产详情
    public static SearchAsset getAssetDetail(Context mContext, String id) {
        SearchAsset searchAsset = null;
        try {
            searchAsset = DaoManager.getDaoSession(mContext)
                    .getSearchAssetDao().queryBuilder().where(SearchAssetDao.Properties.Id_.eq(id)).unique();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return searchAsset;
    }

    public static List<Type> getTypeList(Context mContext) {
        List<Type> typeList = new ArrayList<>();
        try {
            Type type = new Type();
            type.setId_("");
            type.setName("请选择类型");
            typeList.add(type);

            List<Type> types = DaoManager.getDaoSession(mContext).getTypeDao().loadAll();
            if (types != null && types.size() > 0) {
                typeList.addAll(types);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return typeList;
    }

    public static List<Brand> getBrandList(Context mContext) {
        List<Brand> brandList = new ArrayList<>();
        try {
            Brand brand = new Brand();
            brand.setId_("");
            brand.setName("请选择品牌");
            brandList.add(brand);
            List<Brand> brands = DaoManager.getDaoSession(mContext).getBrandDao().loadAll();
            if (brands != null && brands.size() > 0) {
                brandList.addAll(brands);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return brandList;
    }

    public static List<Model> getModelList(Context mContext) {
        List<Model> modelList = new ArrayList<>();
        Model model = new Model();
        model.setBrandId_("");
        model.setTypeId_("");
        model.setId_("");
        model.setName("请选择型号");
        modelList.add(model);
        List<Model> models = DaoManager.getDaoSession(mContext).getModelDao().loadAll();
        if (models != null && models.size() > 0) {
            modelList.addAll(models);
        }
        return modelList;
    }

    public static List<Room> getRoomList(Context mContext) {
        List<Room> roomList = new ArrayList<>();
        try {
            Room room = new Room();
            room.setId_("");
            room.setName("请选择地点");
            roomList.add(room);
            List<Room> rooms = DaoManager.getDaoSession(mContext).getRoomDao().loadAll();
            if (rooms != null && rooms.size() > 0) {
                roomList.addAll(rooms);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return roomList;
    }

    public static Bitmap getBitmap(String url) {
        Bitmap bitmap = null;
        try {
            bitmap = ImageUtils.getNativeImage(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static List<User> getUserList(Context mContext) {
        List<User> userList = new ArrayList<>();
        try {

            User user = new User();
            user.setId_("");
            user.setName("请选择使用人");
            userList.add(user);
            List<User> users = DaoManager.getDaoSession(mContext).getUserDao().loadAll();
            if (users != null) {
                userList.addAll(users);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    /**
     * 根据值, 设置spinner默认选中:
     *
     * @param spinner
     * @param value
     */
    public static void setSpinnerItemSelectedByValue(Spinner spinner, String value) {
        if (value == null) return;
        SpinnerAdapter apsAdapter = spinner.getAdapter(); //得到SpinnerAdapter对象
        int k = apsAdapter.getCount();
        for (int i = 0; i < k; i++) {
            if (value.equals(apsAdapter.getItem(i).toString())) {
                spinner.setSelection(i, true);// 默认选中项
                break;
            }
        }
    }

    public static void queryModelByBrandAndType(Context mContext, String brandId, String typeId) {
        try {
            List<Model> modelList = new ArrayList<>();
            Model model = new Model();
            model.setBrandId_("");
            model.setTypeId_("");
            model.setId_("");
            model.setName("请选择型号");
            modelList.add(model);
            List<Model> models = DaoManager.getDaoSession(mContext)
                    .getModelDao().queryBuilder().where(
                            ModelDao.Properties.BrandId_.eq(brandId),
                            ModelDao.Properties.TypeId_.eq(typeId)).list();
            if (models != null) {
                modelList.addAll(models);
            }
//            assetDetailView.onQueryModelListSuccess(modelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取部门列表
     *
     * @return
     */
    public List<Department> getDepartment() {
        List<Department> departments = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getDepartmentDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (departments == null) {
            departments = new ArrayList<>();
        }
        Department model = new Department();
        model.setId_("");
        model.setName("请选择部门");
        model.setPid_("");
        departments.add(0, model);
        return departments;
    }

    public static void saveToLocal(Context mContext, IAssetDetailView assetDetailView, SearchAsset searchAsset){
        try {
            DaoManager.getDaoSession(mContext).getSearchAssetDao().update(searchAsset);
            assetDetailView.onSaveLocalSuccess(searchAsset);
        }catch (Exception e){
            assetDetailView.onSaveLocalFail(new BusinessErrorException("保存本地数据失败"));
        }

    }

}
